package com.kla.power.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kla.power.entity.AlarmEntity;
import com.kla.power.mapper.AlarmMapper;
import com.kla.power.mapper.UserMapper;
import com.kla.power.service.IAlarmService;
import com.kla.power.util.SnowflakeIdWorker;
import com.kla.power.util.StateEnum;
import com.kla.power.util.StateException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 报警/故障 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2023-06-23
 */
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, AlarmEntity> implements IAlarmService {

    @Resource
    private AlarmMapper alarmMapper;

    @Override
    public List<AlarmEntity> queryList ( AlarmEntity alarmEntity ) {
        if ( alarmEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( alarmEntity.getStationName () ) ) {
            alarmEntity.setStationName ( null );
        }
        alarmEntity.setPage ( ( alarmEntity.getPage () - 1 ) * alarmEntity.getLimit () );
        List<AlarmEntity> alarmEntities = alarmMapper.queryList ( alarmEntity );
        return alarmEntities;
    }

    @Override
    public Integer queryCount ( AlarmEntity alarmEntity ) {
        if ( StringUtils.isBlank ( alarmEntity.getStationName () ) ) {
            alarmEntity.setStationName ( null );
        }
        Integer count = alarmMapper.queryCount ( alarmEntity );
        return count;
    }

    @Override
    public AlarmEntity saveSingle ( AlarmEntity alarmEntity ) {
        alarmEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        alarmEntity.setCreateTime ( new Date () );
        Integer integer = alarmMapper.saveSingle ( alarmEntity );
        if ( integer == 0 ) {
            throw new StateException ( StateEnum.INSERT_EXCE );
        }
        return alarmEntity;
    }

    @Override
    public AlarmEntity querySingle ( AlarmEntity alarmEntity ) {
        //1. 查单个报警详情
        if ( alarmEntity == null ) {
            throw new StateException ( StateEnum.REQUEST_PARAM_EXCE );
        }
        if ( StringUtils.isBlank ( alarmEntity.getId () ) ) {
            throw new StateException ( StateEnum.PRIMARY_NULL_EXCE );
        }
        AlarmEntity alarm = alarmMapper.querySingle ( alarmEntity );
        return alarm;
    }

    /**
     * 大屏统计，按电站分组，查询拥有告警的电站
     * @param alarmEntity
     * @return
     */
    @Override
    public List<AlarmEntity> queryScreeAlarm (AlarmEntity alarmEntity) {
        List<AlarmEntity> alarmEntities = alarmMapper.queryScreeAlarm ( alarmEntity );
        return alarmEntities;
    }


    /**
     * 查询一个电站下所有告警列表
     * @param alarmEntity
     * @return
     */
    @Override
    public List<AlarmEntity> queryAlarmByStation (AlarmEntity alarmEntity) {
        List<AlarmEntity> alarmEntities = alarmMapper.queryAlarmByStation ( alarmEntity );
        return alarmEntities;
    }

    @Override
    public List<AlarmEntity> queryTodayList ( AlarmEntity alarmEntity ) {
        List<AlarmEntity> alarmEntities = alarmMapper.queryTodayList ( alarmEntity );
        return alarmEntities;
    }

}
